package com.homedo.microservice.open.service.gateway.service;

import com.google.common.collect.Lists;
import com.homedo.common.bean.vo.context.UserContext;
import com.homedo.common.dao.bean.po.base.BasePo;
import com.homedo.common.dao.bean.po.enhanced.base.BaseEnhanced;
import com.homedo.common.service.BaseService;
import com.homedo.common.util.Detect;
import com.homedo.microservice.open.service.gateway.bean.enums.forroute.StatusEnum4Route;
import com.homedo.microservice.open.service.gateway.bean.po.RouteVersion;
import com.homedo.microservice.open.service.gateway.bean.po.enhanced.RouteEnhanced;
import com.homedo.microservice.open.service.gateway.bean.po.enhanced.RouteServiceEnhanced;
import com.homedo.microservice.open.service.gateway.bean.po.enhanced.RouteVersionEnhanced;
import com.homedo.microservice.open.service.gateway.persistence.dao.RouteVersionDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 作者 jgshun
 * @date 创建时间 2018年09月19日 13时40分00秒
 */
@Service
public class RouteVersionService extends BaseService<RouteVersion> {
    @Autowired
    private RouteVersionDao routeVersionDao;
    @Autowired
    private RouteService routeService;
    @Autowired
    private RouteServiceService routeServiceService;

    @Override
    public List<RouteVersionEnhanced> getEnhanceds(List<?> ids, UserContext userContext) {
        List<RouteVersion> routeVersions = routeVersionDao.getListByIds(ids, RouteVersion.class);

        if (!Detect.notEmpty(routeVersions)) {
            return null;
        }

        List<RouteVersionEnhanced> routeVersionEnhanceds = this.convent2Enhanceds(routeVersions);

        this.appendEnhancedCommons(routeVersionEnhanceds, userContext);

        return routeVersionEnhanceds;
    }


    @Override
    public List<RouteVersionEnhanced> getEnhanceds4Active(List<?> ids, UserContext userContext) {
        List<RouteVersion> routeVersions = routeVersionDao.getListByIdsGreaterThanMark0(ids, RouteVersion.class);

        if (!Detect.notEmpty(routeVersions)) {
            return null;
        }

        List<RouteVersionEnhanced> routeVersionEnhanceds = this.convent2Enhanceds(routeVersions);

        this.appendEnhancedCommons(routeVersionEnhanceds, userContext);

        return routeVersionEnhanceds;
    }


    @Override
    public List<RouteVersionEnhanced> convent2Enhanceds(List<? extends BasePo> pos) {
        List<RouteVersion> routeVersions = (List<RouteVersion>) pos;
        List<RouteVersionEnhanced> routeVersionEnhanceds = Lists.newArrayList();
        routeVersions.forEach(e -> routeVersionEnhanceds.add(new RouteVersionEnhanced(e)));
        return routeVersionEnhanceds;
    }


    public List<RouteVersionEnhanced> getEnhanceds4Route(List<String> routeIds, UserContext userContext) {
        List<RouteVersion> routeVersions = routeVersionDao.getListByKeyValues(RouteVersion.FIELD_ROUTEID, routeIds, RouteVersion.class);

        if (!Detect.notEmpty(routeVersions)) {
            return null;
        }

        List<RouteVersionEnhanced> routeVersionEnhanceds = this.convent2Enhanceds(routeVersions);

        this.appendEnhancedCommons(routeVersionEnhanceds, userContext);

        return routeVersionEnhanceds;
    }

    public List<RouteVersionEnhanced> getEnhanceds4Application(String applicationName, List<StatusEnum4Route> statusEnum4Routes, UserContext userContext) {
        List<RouteEnhanced> routeEnhanceds = routeService.getEnhanceds4Application(applicationName, statusEnum4Routes, userContext);
        if (!Detect.notEmpty(routeEnhanceds)) {
            return null;
        }
        List<RouteVersionEnhanced> routeVersionEnhanceds = Lists.newArrayList();
        routeEnhanceds.forEach(e -> {
            if (Detect.notEmpty(e.getRouteVersionEnhanceds())) {
                routeVersionEnhanceds.addAll(e.getRouteVersionEnhanceds());
            }
        });

        this.appendEnhancedCommons(routeVersionEnhanceds, userContext);

        return routeVersionEnhanceds;
    }

    @Override
    public List<RouteVersionEnhanced> appendEnhancedCommons(List<? extends BaseEnhanced> enhancedItems, UserContext userContext) {
        List<RouteVersionEnhanced> routeVersionEnhanceds = (List<RouteVersionEnhanced>) enhancedItems;

        this.appendRouteServiceEnhanced(routeVersionEnhanceds, userContext);

        return routeVersionEnhanceds;
    }

    public List<RouteVersionEnhanced> appendRouteServiceEnhanced(List<RouteVersionEnhanced> routeVersionEnhanceds, UserContext userContext) {
        List<String> routeServiceIds = routeVersionEnhanceds.stream()
                .map(RouteVersionEnhanced::getRouteServiceEnhanced).filter(e -> e != null && Detect.notEmpty(e.getId())).collect(Collectors.toList())
                .stream().map(RouteServiceEnhanced::getId).collect(Collectors.toList());
        if (!Detect.notEmpty(routeServiceIds)) {
            return routeVersionEnhanceds;
        }

        List<RouteServiceEnhanced> routeServiceEnhanceds = routeServiceService.getEnhanceds(routeServiceIds, userContext);

        if (!Detect.notEmpty(routeServiceEnhanceds)) {
            return routeVersionEnhanceds;
        }

        this.appendRouteServiceEnhanced(routeVersionEnhanceds, routeServiceEnhanceds, userContext);

        return routeVersionEnhanceds;
    }

    private void appendRouteServiceEnhanced(List<RouteVersionEnhanced> routeVersionEnhanceds, List<RouteServiceEnhanced> routeServiceEnhanceds, UserContext userContext) {
        routeVersionEnhanceds.forEach(e -> {
            this.appendRouteServiceEnhanced(e, routeServiceEnhanceds, userContext);
        });
    }

    private void appendRouteServiceEnhanced(RouteVersionEnhanced routeVersionEnhanced, List<RouteServiceEnhanced> routeServiceEnhanceds, UserContext userContext) {
        routeServiceEnhanceds.forEach(e -> {
            if (routeVersionEnhanced.getRouteServiceEnhanced() != null && e.getId().equals(routeVersionEnhanced.getRouteServiceEnhanced().getId())) {
                routeVersionEnhanced.setRouteServiceEnhanced(e);
                return;
            }
        });
    }


}
